package teacher.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import tools.MD5Tools;
import teacher.bean.Award;
import teacher.bean.Paper;
import teacher.bean.Patent;
import teacher.bean.Project;
import teacher.bean.Teacher;
import teacher.bean.Midteaexp;
import teacher.bean.Expertclass;
import teacher.bean.Treatise;
import database.dao.*;
import tools.StringDate;

public class teacher {
	
	/**
	 * @category 用于验证教师用户登陆的
	 * @param teacher 前台获取教师对象
	 * @return 匹配成功返回true，否则 返回false
	 */
	public boolean CheckTeacher(Teacher teacher){
		boolean flag=false;
		String sql="select password from teacher where user=?";
		String[]  parameter={String.valueOf(teacher.getUser())};
		MysqlCrud mysqlcurd=new MysqlCrud();	
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		if(lists.size()>0 && null != lists){
			Map map=lists.get(0);
			if(map.get(0).toString().equals(teacher.getPassword()))
			{flag=true;}
		}
		return flag;
	}
	
	/**
	 * @category 用于验证教师登陆权限的
	 * @param teacher 前台获取教师对象
	 * @return 教师登陆权限 int
	 */
	public int CheckTeaPower(Teacher teacher){
		int a=0;
		String sql="select power from teacher where user=?";
		String[]  parameter={String.valueOf(teacher.getUser())};
		MysqlCrud mysqlcurd=new MysqlCrud();	
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		if(lists.size()>0 && null != lists){
			Map map=lists.get(0);
			if(map.get(0).toString().equals("2"));
			 a =Integer.parseInt(map.get(0).toString());
		}
		return a;
	}
	
	/**
	 * @category 用于验证方向管理员登陆权限的
	 * @param teacher 前台获取教师对象
	 * @return 教师登陆权限 int
	 */
	public int CheckLeaPower(Teacher teacher){
		int a=0;
		String sql="select power from teacher where user=?";
		String[]  parameter={String.valueOf(teacher.getUser())};
		MysqlCrud mysqlcurd=new MysqlCrud();	
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		if(lists.size()>0 && null != lists){
			Map map=lists.get(0);
			if(map.get(0).toString().equals("1"));
			 a =Integer.parseInt(map.get(0).toString());
		}
		return a;
	}
	
	/**
	 * @category 用于验证学科管理员登陆权限的
	 * @param teacher 前台获取教师对象
	 * @return 教师登陆权限 int
	 */
	public int CheckAdmPower(Teacher teacher){
		int a=0;
		String sql="select power from teacher where user=?";
		String[]  parameter={String.valueOf(teacher.getUser())};
		MysqlCrud mysqlcurd=new MysqlCrud();	
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		if(lists.size()>0 && null != lists){
			Map map=lists.get(0);
			if(map.get(0).toString().equals("0"));
			 a =Integer.parseInt(map.get(0).toString());
		}
		return a;
	}
	
	/**
	 * @category 查询所有的教师
	 * @return teach Teacher 返回一个List<Teacher>的教师对象集合
	 */
	public List<Teacher> SelectAllTeacher(){
		List<Teacher> teach = new ArrayList<Teacher>();
		MysqlCrud mysql = new MysqlCrud();
		String sql = null;
		List<Map> list = null;
			sql = "select * from teacher";
			String[] parameter=null;
			list = mysql.SelectData(sql, parameter);	
		      int i = 0;
		   while(i < list.size()) {
			Map map=list.get(i);
			teach.add(new MapToBean().MapToTeacher(map));
			i++;
		}
		return teach;
	}
	
	/**
	 * @category 按工号查询教师
	 * @param teacher Teacher 传入教师的工号
	 * @return teach Teacher 返回一个教师对象
	 */
	public Teacher SelectTeacher(Teacher teacher){		
		Teacher t=new Teacher();
		String sql="select * from teacher where user=?";
		int i=0;	
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={String.valueOf(teacher.getUser())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
        if(1==list.size()){
			
			Map map=list.get(0);
			t= new MapToBean().MapToTeacher(map);
		
		}
		return t;
	}
	
	
	/**
	 * @category 按论文查询教师
	 * @param teacher Teacher 传入教师的类
	 * @return teach Teacher 返回一个教师对象
	 */
	public Teacher SelectTeaByPap(Paper pp){		
		Teacher teacher1 = new Teacher();
		Teacher teach = new Teacher();
		int aa=pp.getTeaId();
		MysqlCrud mysql = new MysqlCrud();
		String sql = null;
		List<Map> list = null;	
		
	    sql = "select * from teacher where teaId="+aa+"";
		String[] parameter={String.valueOf(pp.getTeaId())};
		System.out.println(parameter[0]);
		list = mysql.SelectData(sql, null);

		if(1 == list.size()) {
			Map map=list.get(0);
			teacher1=new MapToBean().MapToTeacher(map);
			teach = teacher1;
		} else if(0 == list.size()){
			teach = null;
		}	
		return teach;
	}
	
	/**
	 * @category 按专利查询教师
	 * @param teacher Teacher 传入教师的类
	 * @return teach Teacher 返回一个教师对象
	 */
	public Teacher SelectTeaByPat(Patent pp){		
		Teacher teacher1 = new Teacher();
		Teacher teach = new Teacher();
		int aa=pp.getTeaId();
		MysqlCrud mysql = new MysqlCrud();
		String sql = null;
		List<Map> list = null;	
		
	    sql = "select * from teacher where teaId="+aa+"";
		String[] parameter={String.valueOf(pp.getTeaId())};
		System.out.println(parameter[0]);
		list = mysql.SelectData(sql, null);

		if(1 == list.size()) {
			Map map=list.get(0);
			teacher1=new MapToBean().MapToTeacher(map);
			teach = teacher1;
		} else if(0 == list.size()){
			teach = null;
		}	
		return teach;
	}
	
	/**
	 * @category 按论著查询教师
	 * @param teacher Teacher 传入教师的类
	 * @return teach Teacher 返回一个教师对象
	 */
	public Teacher SelectTeaByTre(Treatise pp){		
		Teacher teacher1 = new Teacher();
		Teacher teach = new Teacher();
		int aa=pp.getTeaId();
		MysqlCrud mysql = new MysqlCrud();
		String sql = null;
		List<Map> list = null;	
		
	    sql = "select * from teacher where teaId="+aa+"";
		String[] parameter={String.valueOf(pp.getTeaId())};
		System.out.println(parameter[0]);
		list = mysql.SelectData(sql, null);

		if(1 == list.size()) {
			Map map=list.get(0);
			teacher1=new MapToBean().MapToTeacher(map);
			teach = teacher1;
		} else if(0 == list.size()){
			teach = null;
		}	
		return teach;
	}
	
	/**
	 * @category 按获奖查询教师
	 * @param teacher Teacher 传入教师的类
	 * @return teach Teacher 返回一个教师对象
	 */
	public Teacher SelectTeaByAwa(Award pp){		
		Teacher teacher1 = new Teacher();
		Teacher teach = new Teacher();
		int aa=pp.getTeaId();
		MysqlCrud mysql = new MysqlCrud();
		String sql = null;
		List<Map> list = null;	
		
	    sql = "select * from teacher where teaId="+aa+"";
		String[] parameter={String.valueOf(pp.getTeaId())};
		System.out.println(parameter[0]);
		list = mysql.SelectData(sql, null);

		if(1 == list.size()) {
			Map map=list.get(0);
			teacher1=new MapToBean().MapToTeacher(map);
			teach = teacher1;
		} else if(0 == list.size()){
			teach = null;
		}	
		return teach;
	}
	
	/**
	 * @category 按项目查询教师
	 * @param teacher Teacher 传入教师的类
	 * @return teach Teacher 返回一个教师对象
	 */
	public Teacher SelectTeaByPro(Project pp){		
		Teacher teacher1 = new Teacher();
		Teacher teach = new Teacher();
		int aa=pp.getTeaId();
		MysqlCrud mysql = new MysqlCrud();
		String sql = null;
		List<Map> list = null;	
		
	    sql = "select * from teacher where teaId="+aa+"";
		String[] parameter={String.valueOf(pp.getTeaId())};
		System.out.println(parameter[0]);
		list = mysql.SelectData(sql, null);

		if(1 == list.size()) {
			Map map=list.get(0);
			teacher1=new MapToBean().MapToTeacher(map);
			teach = teacher1;
		} else if(0 == list.size()){
			teach = null;
		}	
		return teach;
	}
	
	
	
	
	/**
	 * @category 用于教师息的修改
	 * @param teacherOne 教师修改的内容
	 * @param teacherTwo 定位教师修改的位置
	 * @return 修改成功，返回true，否则返回flase
	 */
	public boolean updateTeacher(Teacher teacherOne,Teacher teacherTwo) {
		String sql= "update teacher set teaId=?,name=?,password=?,subject=?,direction=?,duty=? ,degree=?,online=?,birthday=?,furStud=?,topSchool=?,topMajor=?,mail=?,power=?,inSubDate=? where user="+teacherTwo.getUser()+"";
		String[] parameter = {
				String.valueOf(teacherOne.getTeaId()),		
				String.valueOf(teacherOne.getName()),
				String.valueOf(teacherOne.getPassword()),
				String.valueOf(teacherOne.getSubject()),
				String.valueOf(teacherOne.getDirection()),
				String.valueOf(teacherOne.getDuty()),
				String.valueOf(teacherOne.getDegree()),
				String.valueOf(teacherOne.getOnline()),
				String.valueOf(new StringDate().DateToString(teacherOne.getBirthday())),
				String.valueOf(teacherOne.getFurStud()),
				String.valueOf(teacherOne.getTopSchool()),
				String.valueOf(teacherOne.getTopMajor()),
				String.valueOf(teacherOne.getMail()),
				String.valueOf(teacherOne.getPower()),
				String.valueOf(new StringDate().DateToString(teacherOne.getInSubDate()))
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 用于教师密码的修改
	 * @param teacherOne 教师修改的内容
	 * @return 修改成功，返回true，否则返回flase
	 */
	public boolean updateTeacherPas(Teacher t,String a) {
		String sql= "update teacher set password=? where user="+t.getUser()+"";
		String[] parameter = {
							
				String.valueOf(a),
				
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	
	/**
	 * @category 用于删除教师的信息
	 * @param teacher 将要删除的教师信息
	 * @return 删除成功返回true，否则为false
	 */
	public boolean DeleteTeacher(Teacher teacher){
		String sql = "delete from teacher where user=?";
		String[]  parameter={String.valueOf(teacher.getUser())};
		 MysqlCrud mysqlcurd=new MysqlCrud();
		 boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		 return flag;
	}
	
	/**
	 * @category 统计教师的数量
	 * @return sum int 教师的数量
	 */
		public int getCountTeacher()
		{
			int sum=0;
			String sql="select count(*) from teacher";
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> lists=mysqlcurd.SelectData(sql, null);
			Map map=lists.get(0);
			sum=Integer.parseInt(map.get(0).toString());
			return sum;
		}
		
		/**
		 * @category 查询所有学历为博士的教师
		 * @return teach Teacher 返回一个List<Teacher>的教师对象集合
		 */
		public List<Teacher> SelectDoctor(){
			List<Teacher> teach = new ArrayList<Teacher>();
			MysqlCrud mysql = new MysqlCrud();
			String sql = null;
			List<Map> list = null;
				sql = "select * from teacher where degree=0";
				String[] parameter=null;
				list = mysql.SelectData(sql, parameter);	
			System.out.println("aaaaaaaaaa"+list.size());
			      int i = 0;
			   while(i < list.size()) {
				Map map=list.get(i);
				teach.add(new MapToBean().MapToTeacher(map));
				i++;
			}
			return teach;
		}
		
		/**
		 * @category 统计教师学位是博士的数量
		 * @return sum int 教师的数量
		 */
			public int getCountDoctor()
			{
				int sum=0;
				String sql="select count(*) from teacher where degree=0";
				MysqlCrud mysqlcurd=new MysqlCrud();
				List<Map> lists=mysqlcurd.SelectData(sql, null);
				Map map=lists.get(0);
				sum=Integer.parseInt(map.get(0).toString());
				return sum;
			}
			
			/**
			 * @category 统计教师学位是硕士的数量
			 * @return sum int 教师的数量
			 */
				public int getCountMaster()
				{
					int sum=0;
					String sql="select count(*) from teacher where degree=1";
					MysqlCrud mysqlcurd=new MysqlCrud();
					List<Map> lists=mysqlcurd.SelectData(sql, null);
					Map map=lists.get(0);
					sum=Integer.parseInt(map.get(0).toString());
					return sum;
				}
				
				/**
				 * @category 统计教师学位是学士的数量
				 * @return sum int 教师的数量
				 */
					public int getCountScholar()
					{
						int sum=0;
						String sql="select count(*) from teacher where degree=2";
						MysqlCrud mysqlcurd=new MysqlCrud();
						List<Map> lists=mysqlcurd.SelectData(sql, null);
						Map map=lists.get(0);
						sum=Integer.parseInt(map.get(0).toString());
						return sum;
					}
		
					/**
					 * @category 统计在职教师数量
					 * @return sum int 教师的数量
					 */
						public int getCountOnline()
						{
							int sum=0;
							String sql="select count(*) from teacher where online=0";
							MysqlCrud mysqlcurd=new MysqlCrud();
							List<Map> lists=mysqlcurd.SelectData(sql, null);
							Map map=lists.get(0);
							sum=Integer.parseInt(map.get(0).toString());
							return sum;
						}
						
						/**
						 * @category 统计不在职教师数量
						 * @return sum int 教师的数量
						 */
							public int getCountOffline()
							{
								int sum=0;
								String sql="select count(*) from teacher where online=1";
								MysqlCrud mysqlcurd=new MysqlCrud();
								List<Map> lists=mysqlcurd.SelectData(sql, null);
								Map map=lists.get(0);
								sum=Integer.parseInt(map.get(0).toString());
								return sum;
							}
		/**
		 * @category 查询所有学历为硕士的教师
		 * @return teach Teacher 返回一个List<Teacher>的教师对象集合
		 */
		public List<Teacher> SelectMaster(){
			List<Teacher> teach = new ArrayList<Teacher>();
			MysqlCrud mysql = new MysqlCrud();
			String sql = null;
			List<Map> list = null;
				sql = "select * from teacher where degree=1";
				String[] parameter=null;
				list = mysql.SelectData(sql, parameter);	
			System.out.println("aaaaaaaaaa"+list.size());
			      int i = 0;
			   while(i < list.size()) {
				Map map=list.get(i);
				teach.add(new MapToBean().MapToTeacher(map));
				i++;
			}
			return teach;
		}

		/**
		 * @category 查询所有学历为学士的教师
		 * @return teach Teacher 返回一个List<Teacher>的教师对象集合
		 */
		public List<Teacher> SelectScholar(){
			List<Teacher> teach = new ArrayList<Teacher>();
			MysqlCrud mysql = new MysqlCrud();
			String sql = null;
			List<Map> list = null;
				sql = "select * from teacher where degree=2";
				String[] parameter=null;
				list = mysql.SelectData(sql, parameter);	
			System.out.println("aaaaaaaaaa"+list.size());
			      int i = 0;
			   while(i < list.size()) {
				Map map=list.get(i);
				teach.add(new MapToBean().MapToTeacher(map));
				i++;
			}
			return teach;
		}
		
		/**
		 * @category 查询所有状态为已报省厅的教师
		 * @return teach Teacher 返回一个List<Teacher>的教师对象集合
		 */
		public List<Teacher> SelectState0Teacher(){
			List<Teacher> teach = new ArrayList<Teacher>();
			MysqlCrud mysql = new MysqlCrud();
			String sql = null;
			List<Map> list = null;
				sql = "select * from teacher where state=0";
				String[] parameter=null;
				list = mysql.SelectData(sql, parameter);	
			System.out.println("aaaaaaaaaa"+list.size());
			      int i = 0;
			   while(i < list.size()) {
				Map map=list.get(i);
				teach.add(new MapToBean().MapToTeacher(map));
				i++;
			}
			return teach;
		}
		
		/**
		 * @category 查询所有在职的教师
		 * @return teach Teacher 返回一个List<Teacher>的教师对象集合
		 */
		public List<Teacher> SelectOnlineTeacher(){
			List<Teacher> teach = new ArrayList<Teacher>();
			MysqlCrud mysql = new MysqlCrud();
			String sql = null;
			List<Map> list = null;
				sql = "select * from teacher where online=0";
				String[] parameter=null;
				list = mysql.SelectData(sql, parameter);	
			System.out.println("aaaaaaaaaa"+list.size());
			      int i = 0;
			   while(i < list.size()) {
				Map map=list.get(i);
				teach.add(new MapToBean().MapToTeacher(map));
				i++;
			}
			return teach;
		}
		
		/**
		 * @category 查询所有不在职的教师
		 * @return teach Teacher 返回一个List<Teacher>的教师对象集合
		 */
		public List<Teacher> SelectOfflineTeacher(){
			List<Teacher> teach = new ArrayList<Teacher>();
			MysqlCrud mysql = new MysqlCrud();
			String sql = null;
			List<Map> list = null;
				sql = "select * from teacher where online=1";
				String[] parameter=null;
				list = mysql.SelectData(sql, parameter);	
			System.out.println("aaaaaaaaaa"+list.size());
			      int i = 0;
			   while(i < list.size()) {
				Map map=list.get(i);
				teach.add(new MapToBean().MapToTeacher(map));
				i++;
			}
			return teach;
		}
		
		/**
		 * @category 插入教师专家类别信息
		 * @param teacher Teacher 将要插入的教师信息
		 * @param midteaexp Midteaexp 将要插入的专家类别
		 * @return 插入成功返回true，否则为false
		 */
		public boolean InsertTeacher(Teacher teacher,Midteaexp midteaexp){
			String sql = "insert into midteaexp(teaId,expId) value(?,?)";
			String[] parameter = {
					String.valueOf(teacher.getTeaId()),
					String.valueOf(midteaexp.getExpId()),
			};
			MysqlCrud mysqlcurd=new MysqlCrud();
			boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
			return flag;
		}
		
		/**
		 * @category 查询一个老师的所有专家类别
		 * @param  teacher Teacher 需要被查询的教师信息
		 * @return exp Expertclass 返回一个List<Expertclass>的专家类别对象集合
		 */
		public List<Expertclass> selectExpertclassByTea(Teacher teacher)
		{
			String sql="select * from expertclass as e,teacher as t,midteaexp as m where t.teaId=m.teaId and e.expId=m.expId";
			List<Expertclass> expertclass=new ArrayList<Expertclass>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			String[]  parameter={String.valueOf(teacher.getTeaId())};
			List<Map> lists=mysqlcurd.SelectData(sql, parameter);
			for(int i=0;i<lists.size();i++)
			{
				Map map=lists.get(i);
	           try {
	        	   Expertclass exp=new Expertclass();
					exp.setExpId(Integer.parseInt(map.get(0).toString()));
					exp.setExpert(map.get(1).toString());
					expertclass.add(exp);	
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
			return expertclass;
		}	
		
		
		/**
		 * @category 查询一个老师的所有专家类别Id
		 * @param  teacher Teacher 需要被查询的教师信息
		 * @return exp Expertclass 返回一个List<Expertclass>的专家类别对象集合
		 */
		public List<Expertclass> selectExpertId(Teacher teacher)
		{
			String sql="select teaId from expertclass as e,teacher as t,midteaexp as m where t.teaId=m.teaId and e.expId=m.expId";
			List<Expertclass> expertclass=new ArrayList<Expertclass>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			String[]  parameter={String.valueOf(teacher.getTeaId())};
			List<Map> lists=mysqlcurd.SelectData(sql, parameter);
			for(int i=0;i<lists.size();i++)
			{
				Map map=lists.get(i);
	           try {
	        	   Expertclass exp=new Expertclass();
					exp.setExpId(Integer.parseInt(map.get(0).toString()));
					exp.setExpert(map.get(1).toString());
					expertclass.add(exp);	
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
			return expertclass;
		}	
		
		/**
		 * @category 查询所有专家类别
		 * @return exp Expertclass 返回一个List<Expertclass>的专家类别对象集合
		 */
		public List<Expertclass> selectAllExpertclass() {
			List<Expertclass> list = new ArrayList<Expertclass>();
			String sql="select * from expertclass";
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> lists = mysqlcurd.SelectData(sql, null);
			for(int i=0;i<lists.size();i++) {
				Map map=lists.get(i);
				try {
					Expertclass experter = new Expertclass();
					experter.setExpId(Integer.parseInt(map.get(0).toString()));
					experter.setExpert(map.get(1).toString());
					list.add(experter);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
			return list;
		}
		
		
		/**
		 * @category 查询教师的所有专家类别
		 * @return 返回一个List<int>集合
		 */
		public List<Integer> SelectTeaExp(Teacher t){
		    List<Integer> teaExp=new ArrayList<Integer>();
			MysqlCrud mysql = new MysqlCrud();
			String sql = null;
			List<Map> list = null;
				sql = "select expId from midteaexp where teaId="+t.getTeaId()+"";
				String[] parameter=null;
				list = mysql.SelectData(sql, parameter);	
			      int i = 0;
			   while(i < list.size()) {
				Map map=list.get(i);
				try {
			
					int a;
					a= Integer.parseInt(map.get(0).toString());
					teaExp.add(i, a);
			
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				i++;
			}
			return teaExp;
		}
		
		/**
		 * @category 按Id查询专家类别
		 * @param teacher Teacher 传入教师的类
		 * @return teach Teacher 返回一个教师对象
		 */
		public Expertclass SelectExpById(int a){	
			Expertclass ex=new Expertclass();
			MysqlCrud mysql = new MysqlCrud();
			String sql = null;
			List<Map> list = null;	
			
		    sql = "select * from expertclass where expId="+a+"";
			String[] parameter={String.valueOf(a)};
			list = mysql.SelectData(sql, null);

			if(1 == list.size()) {
				Map map=list.get(0);
				try {			
					ex.setExpId(Integer.parseInt(map.get(0).toString()));
					ex.setExpert(String.valueOf(map.get(1).toString()));
					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}	
			} 
			return ex;
		}

}
